package com.mage.completeproject.listener;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

public class HelloSpringApplicationRunListener implements SpringApplicationRunListener {

    /**
     * 1.SpringApplicationRunListener 接口的作用主要就是在Spring Boot 启动初始化的过程中可以通过
     *   SpringApplicationRunListener接口回调来让用户在启动的各个流程中可以加入自己的逻辑。
     * 2.Spring Boot启动过程的关键事件（按照触发顺序）包括：
     *   (1)开始启动
     *   (2)Environment构建完成
     *   (3)ApplicationContext构建完成
     *   (4)ApplicationContext完成加载
     *   (5)ApplicationContext完成刷新并启动
     *   (6)启动完成
     *   (7)启动失败
     */

    //必须有的构造器
    public HelloSpringApplicationRunListener(SpringApplication application, String[] args){

    }

    //开始启动
    @Override
    public void starting() {
        System.out.println("SpringApplicationRunListener...starting...");
    }

    //Environment构建完成
    @Override
    public void environmentPrepared(ConfigurableEnvironment environment) {
        //获得操作系统名
        Object o = environment.getSystemProperties().get("os.name");
        System.out.println("SpringApplicationRunListener...environmentPrepared.."+o);
    }

    //ApplicationContext构建完成
    @Override
    public void contextPrepared(ConfigurableApplicationContext context) {
        System.out.println("SpringApplicationRunListener...contextPrepared...");
    }

    //ApplicationContext完成加载
    @Override
    public void contextLoaded(ConfigurableApplicationContext context) {
        System.out.println("SpringApplicationRunListener...contextLoaded...");
    }

    //ApplicationContext完成刷新并启动
    @Override
    public void started(ConfigurableApplicationContext context) {
        System.out.println("SpringApplicationRunListener...started...");
    }

    //启动完成
    @Override
    public void running(ConfigurableApplicationContext context) {
        System.out.println("SpringApplicationRunListener...running...");
    }

    //启动失败
    @Override
    public void failed(ConfigurableApplicationContext context, Throwable exception) {
        System.out.println("SpringApplicationRunListener...failed...");
    }
}
